#include "qqcell.h"


QQCell::QQCell(const QString cellname,int offsetcelly,QWidget *parent)
    : QWidget(parent)

{
    offsetCellY=offsetcelly;
    m_onlinecount=0;
    m_groupcount=0;
    m_isOpen=false;
    m_friendtitle=new QQCellTitle(cellname,this);
    m_qqcellcontent=new QWidget(this);

    m_friendtitle->setGeometry(0,0,QQ_CELL_WIDTH,QQ_TITLE_HEIGHT);
    m_qqcellcontent->setGeometry(0,QQ_TITLE_HEIGHT,QQ_CELL_WIDTH,0);
   m_qqcellcontent->hide();
    this->setGeometry(0,offsetCellY,QQ_CELL_WIDTH,QQ_TITLE_HEIGHT);

    connect(m_friendtitle,SIGNAL(sigCellStatusChanged(bool)),this,SLOT(onCellStatuschange(bool)));

}

QQCell::~QQCell()
{

}

void QQCell::addCellFriend(const QString &name, bool isOnline)
{
    QQCellLine*friend1=new QQCellLine(name,isOnline,m_qqcellcontent);
    m_celllines.append(friend1);
    int offsetCellLineY=m_celllines.size()-1;
    friend1->setGeometry(0, offsetCellLineY* QQ_LINE_HEIGHT, QQ_CELL_WIDTH, QQ_LINE_HEIGHT);
    int count=0;
    for(int i=0;i<m_celllines.size();i++)
    {
        if(m_celllines[i]->getstatus()=="当前用户状态：在线")
        {
            count++;
        }
    }

    m_friendtitle->setOnlineAndGroup(count,m_celllines.size());
}

void QQCell::resizeCell(int startY)
{

    int currentHeight = m_isOpen ? (QQ_TITLE_HEIGHT + m_groupcount * QQ_LINE_HEIGHT) : QQ_TITLE_HEIGHT;
    this->setGeometry(0, startY, QQ_CELL_WIDTH, currentHeight);//主要问题是 resizeCell 方法没有考虑展开状态

}
void QQCell::onCellStatuschange(bool isopen)
{

    if(isopen)
    {
         qDebug()<<"发送信号";

        m_groupcount=m_celllines.size();
        int newheight=m_groupcount*QQ_LINE_HEIGHT;
        m_qqcellcontent->setMaximumHeight(newheight);
        m_qqcellcontent->setMinimumHeight(newheight);
        this->setMaximumHeight(newheight+QQ_TITLE_HEIGHT);
        this->setMinimumHeight(newheight+QQ_TITLE_HEIGHT);
        m_qqcellcontent->show();


    }
    else
    {
        m_qqcellcontent->setMinimumHeight(0);
        m_qqcellcontent->setMaximumHeight(0);
        this->setMinimumHeight(QQ_TITLE_HEIGHT);
        this->setMaximumHeight(QQ_TITLE_HEIGHT);
        m_qqcellcontent->hide();

    }
    emit sigCellStatusChange(this);
}
